﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class PurviewManage_RoleAdd : System.Web.UI.Page
{
    IRolePurviewHandler rolewManage = new RolePurviewHandler();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserID"] != null)
            {
                ISystemHandler sysManage = new SystemHandler();
                string strPermission = sysManage.GetLoginUserPermissions();
                if (!(strPermission.Contains("1003") || strPermission.Contains("1004")))
                {
                    Response.Redirect("~/NotPurview.aspx");
                    return;
                }
            }

            dataBind();
        }
    }

    private void dataBind()
    {
          
        DataTable dt = rolewManage.GetAllPurviewList();
        if (dt != null && dt.Rows.Count > 0)
        {
            chkOperationList.DataSource = dt;
            chkOperationList.DataTextField = "PurviewName";
            chkOperationList.DataValueField = "PurviewID";
            chkOperationList.DataBind();
        }

        if (Request["RoleID"] != null)
        {
            DataTable dtRole = rolewManage.GetRoles(int.Parse(Request["RoleID"]));
            if (dtRole != null && dtRole.Rows.Count > 0)
            {
                txtRoleName.Text = dtRole.Rows[0]["RoleName"].ToString();
                txtRoleDes.Text = dtRole.Rows[0]["Description"].ToString();
                txtRoleCode.Text = dtRole.Rows[0]["RoleCode"].ToString();

                string[] permissions = dtRole.Rows[0]["RolePurview"].ToString().Split(new char[] { ',' });
                List<string> lstPermissions = new List<string>();
                lstPermissions.AddRange(permissions);

                foreach (ListItem item in chkOperationList.Items)
                {
                    if (lstPermissions.Contains(item.Value))
                    {
                        item.Selected = true;
                    }
                }
            }
            this.Title = "修改角色信息";
            lblPageType.Text = "角色修改";
            btnSave.Text = "更新";
            btnReset.Text = "返回";
        }
        else
        {
            this.Title = "添加角色信息";
            lblPageType.Text = "角色添加";
            btnSave.Text = "添加";
            btnReset.Text = "返回";
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        RolesQuery roleQuery = new RolesQuery();
        StringBuilder sb = new StringBuilder();
        foreach (ListItem item in chkOperationList.Items)
        {
            if (item.Selected)
            {
                sb.Append(item.Value + ",");
            }
        }
        string permission = sb.ToString();
        if (permission.Length > 0)
        {
            permission = permission.Substring(0, permission.Length - 1);
        }

        if (Request["RoleID"] != null)
        {
                       
            this.Title = "修改角色信息";
            lblPageType.Text = "角色修改";
            btnSave.Text = "更新";
            btnReset.Text = "返回";

            roleQuery.RoleID = int.Parse(Request["RoleID"]);
            roleQuery.RoleName = txtRoleName.Text.Trim();
            roleQuery.RoleCode = txtRoleCode.Text.Trim();
            roleQuery.Description = txtRoleDes.Text.Trim();
            roleQuery.RolePurview = permission;
            if (rolewManage.UpdateRoles(roleQuery))
            {              
                Jscript.AlertAndRedirect("修改角色成功!", "RolesList.aspx", this.Page);
            }
            else
            {
                Jscript.Alert("修改角色失败!", this.Page);
            }
        }
        else
        {
            this.Title = "添加角色信息";
            lblPageType.Text = "角色添加";
            btnSave.Text = "添加";
            btnReset.Text = "返回";

           
            DataTable dtRole = rolewManage.GetRolesList(txtRoleName.Text.Trim());
            if (dtRole != null && dtRole.Rows.Count > 0)
            {
                Jscript.Alert("该角色名已经存在,请重新输入!", this.Page);
                return;
            }
            else
            {
                roleQuery.RoleName = txtRoleName.Text.Trim();
                roleQuery.RoleCode = txtRoleCode.Text.Trim();
                roleQuery.RolePurview = permission;
                roleQuery.Description = txtRoleDes.Text.Trim();
                if (rolewManage.InsertRoles(roleQuery))
                {
                   
                    Jscript.AlertAndRedirect("添加角色成功!", "RolesList.aspx", this.Page);
                }
                else
                {
                    Jscript.Alert("添加角色失败!", this.Page);
                }
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("RolesList.aspx");
    }
}
